package com.blue.gateway.paycore.wxpay.refund;

import com.blue.gateway.annotation.Channel;
import com.blue.gateway.common.Request;
import com.blue.gateway.common.request.RefundRequest;
import com.blue.gateway.config.BaseChannelConfig;
import com.blue.gateway.config.WxConfig;
import com.blue.gateway.paycore.wxpay.SSLConSocketFactory;
import com.blue.gateway.paycore.wxpay.WxPayConstants;
import com.blue.gateway.dto.Result;
import com.blue.gateway.dto.ResultWrapper;
import com.blue.gateway.enums.PayChannelEnum;
import com.blue.gateway.enums.ResultCodeEnum;
import com.blue.gateway.utils.BigDecimalUtil;
import com.blue.gateway.utils.RandomUtil;
import com.blue.gateway.utils.http.HttpClientsUtil;
import com.blue.gateway.utils.sign.WxPaySignature;
import com.blue.gateway.paycore.AbstractBaseChannelService;
import com.blue.gateway.enums.ServiceChannelEnum;
import com.blue.gateway.utils.XmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @author xiaoyiming
 * @date 2020/5/3 19:35
 * @depiction
 */
@Slf4j
@Channel(PayChannel = PayChannelEnum.WX, ServiceChannel = ServiceChannelEnum.REFUND)
public class WxRefundService extends AbstractBaseChannelService {

    @Autowired
    private WxConfig wxConfig;


    @Override
    public Result validate(Request request) {
        RefundRequest refundRequest = (RefundRequest) request;
        if (StringUtils.isBlank(refundRequest.getOutTradeNo())) {
            return ResultWrapper.fail(ResultCodeEnum.PARAM_ILLEGAL);
        }
        if (BigDecimalUtil.ltZeroOrNull(refundRequest.getRefundAmount())) {
            return ResultWrapper.fail(ResultCodeEnum.PARAM_ILLEGAL);
        }

        return ResultWrapper.success();
    }


    @Override
    public BaseChannelConfig getConfig(Request request) {
        return wxConfig;
    }

    @Override
    public String generateParams(Request request, BaseChannelConfig channelConfig) {
        RefundRequest payRequest = (RefundRequest) request;
        WxConfig wxConfig = (WxConfig) channelConfig;

        SortedMap<String, String> params = new TreeMap<>();
        params.put("appid", wxConfig.getAppId());
        params.put("mch_id", wxConfig.getMchId());
        params.put("nonce_str", RandomUtil.getRandomStr());

        if (StringUtils.isNotEmpty(payRequest.getTradeNo())) {
            params.put("transaction_id", payRequest.getTradeNo());
        }
        if (StringUtils.isNotEmpty(payRequest.getOutTradeNo())) {
            params.put("out_trade_no", payRequest.getOutTradeNo());
        }
        params.put("out_refund_no", payRequest.getOutRequestNo());
        params.put("total_fee", BigDecimalUtil.toUnitFen(payRequest.getTotalAmount()));
        params.put("refund_fee", BigDecimalUtil.toUnitFen(payRequest.getRefundAmount()));
        params.put("notify_url", payRequest.getNotifyUrl());

        String sign = WxPaySignature.sign(params, wxConfig.getParterKey());
        //   String sign = WxSignature.sign(params, wxConfig.getParterKey());
        params.put("sign", sign);
        return XmlUtil.mapToXML(params);
    }

    //没有证书 未校验
    @Override
    public String execute(String requestParams, BaseChannelConfig channelConfig) {
        String executeResult = null;
        WxConfig wxConfig = (WxConfig) channelConfig;
        try {
            SSLConnectionSocketFactory sslFactory = SSLConSocketFactory.get(wxConfig.getKeyPath(), wxConfig.getMchId());
            executeResult = HttpClientsUtil.httpClientSLLPost(sslFactory, WxPayConstants.WX_REFUND_GATEWAY, requestParams);
        } catch (Exception e) {
            //
        }

        return executeResult;
    }

    @Override
    public Result analysisResult(String executeResult) {
        return null;
    }
}
